#include "sync.h"
#include "ui_sync.h"

sync::sync(QHash<QString, NoteT> *hashNote,QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::sync)
{
    ui->setupUi(this);
    this->setWindowTitle("同步便签");
    nt = hashNote;
    QString url = nt->value("_nt_").content;
    backGroundColor = nt->value("_nt_").backGroundColor;
    fontColor = nt->value("_nt_").fontColor;
    this->setcolor();
    ui->lineEdit->setText(url);
}

sync::~sync()
{
    delete ui;
}

/**
 * 设置主题
**/
void sync::setcolor(){
    this->setStyleSheet("background-color:"+ this->backGroundColor.name() +
                        "; color: "+ this->fontColor.name() +";");
}

/**
 * 克隆仓库
**/
void sync::on_pushButton_3_clicked()
{
    QProcess gitclone;
    gitclone.start("git", QStringList() << "clone" <<ui->lineEdit->text()<<"tmp");
    (*nt)["_nt_"].content = ui->lineEdit->text();
    // 等待进程完成
    gitclone.waitForFinished();
    QString msg = QString::fromUtf8(gitclone.readAllStandardError());
    QString msgout = QString::fromUtf8(gitclone.readAllStandardOutput());
    if (gitclone.exitCode() == 0){
        if(msg.contains("ssh-askpass")){
            QMessageBox::information(this,"错误","请安装ssh-askpass\n "
                                                   "安装命令:\nsudo apt install ssh-askpass\n");
        }else if(msg.contains("git")){
            QMessageBox::information(this,"错误","请安装git\n "
                                                   "安装命令:\nsudo apt install git\n");
        }else if(msg.contains("正克隆到")){
            QDir dir,rtmp("tmp");
            QDir notedir("notes/.git");
            notedir.removeRecursively();
            if(dir.rename("tmp/.git","notes/.git")){
                if(!rtmp.removeRecursively()){
                    QMessageBox::information(this,"失败","删除tmp目录失败");
                }else{
                    // 检出文件
                    QProcess gitcheckout;
                    QStringList arguments;
                    arguments.clear();
                    arguments << "-C" << gitRepositoryPath << "checkout" <<".";
                    gitcheckout.start(program, arguments);
                    gitcheckout.waitForFinished();
                    emit sigHashInit();
                    QMessageBox::information(this,"成功","克隆成功！");

                }
            }else{
                QMessageBox::information(this,"失败","git 移动失败！");
            }
        }else{
            QMessageBox::information(this,"错误",msg);
        }
    }else{
        QMessageBox::information(this,"错误","git "+gitclone.errorString());
    }
}

/**
 * 上传
**/
void sync::on_pushButton_clicked()
{
    QProcess process;
    QStringList arguments;

    // 添加文件到暂存区
    arguments.clear();
    arguments << "-C" << gitRepositoryPath << "add" << filesToAdd;
    process.start(program, arguments);
    process.waitForFinished();

    // 提交更改
    arguments.clear();
    arguments << "-C" << gitRepositoryPath << "commit" << "-m" <<nt->value("_nt_").status;
    process.start(program, arguments);
    process.waitForFinished();
    (*nt)["_nt_"].status = "";
    emit sigClose();

    // 推送到远程仓库
    arguments.clear();
    arguments << "-C" << gitRepositoryPath << "push";
    process.start(program, arguments);
    process.waitForFinished();

    // 检查命令是否成功执行
    if (process.exitCode() == 0) {
        QMessageBox::information(this,"成功","上传成功!");
    }else{
        QMessageBox::information(this,"失败","git "+process.errorString());
    }

}


/**
 * 下载
**/
void sync::on_pushButton_2_clicked()
{
    QString gitRepositoryPath = "notes";
    QString filesToAdd = "notes/*";

    QProcess process;
    QString program = "git"; // 假设 Git 已经在系统的 PATH 中
    QStringList arguments;

    arguments.clear();
    arguments << "-C" << gitRepositoryPath << "pull";
    process.start(program, arguments);
    process.waitForFinished();


    // 推送到远程仓库
    arguments.clear();
    arguments << "-C" << gitRepositoryPath << "checkout" <<".";
    process.start(program, arguments);
    process.waitForFinished();

    // 检查命令是否成功执行
    if (process.exitCode() == 0) {
        emit sigHashInit();
        QMessageBox::information(this,"更新","下载成功!");
    }else{
         QMessageBox::information(this,"失败","git "+process.errorString());
    }
}
